<?php
require_once 'BaseController.php';
class Admin_IndexController extends BaseController
{
    public $_email = null;
    public $_role = null;
    public function init()
    {
        $this->view->module = $this->_request->getModuleName();
        $this->view->controller = $this->_request->getControllerName();
        $this->view->action = $this->_request->getActionName();

        Zend_Loader::loadClass('AuthModel');
        Zend_Loader::loadClass('AdminModel');
        Zend_Loader::loadClass('FilmInfoModel');
        Zend_Loader::loadClass('ParentTypeModel');
        Zend_Loader::loadClass('RemoveCircumflex');
        
        $parentType = new ParentTypeModel();
        $list = $parentType->getListTypeForAdmin();
        $this->view->listType = $list;

        Zend_Loader::loadClass('AuthModel');
        $auth = new AuthModel();
        $this->_email = $auth->getIdentityField('sunnet_email');
        $this->_role = $auth->getIdentityField('role');
    }
    public function indexAction()
    {
        if (isset($_GET['ajax'])) {
            $options = array('json' => 1);
            $model = new FilmInfoModel();
            echo $model->getListFilmAdmin('', $options);
            exit();
        }
    }
    public function addAction()
    {

        if ($this->_request->isPost()) {
            $model = new FilmInfoModel();
            $number_ep = $this->_request->getParam('number_ep');
            $add_ep = $this->_request->getParam('add_ep');
            $del_ep = $this->_request->getParam('del_ep');
            $type_data_ep = $this->_request->getParam('type_data_ep');
            
            for($i =1; $i <= $add_ep; $i++){
                if($this->_request->getParam('ep-' . $i)!= "" && $this->_request->getParam('ep-' . $i)!= null){
                    $url_eps[] = stripslashes($this->_request->getParam('ep-' . $i));
                    $url_mp3[] = stripslashes($this->_request->getParam('mp3-' . $i));
// tien hanh bam lai chuoi mp3 cho nhac
                 
                    $des_eps[] = $this->_request->getParam('des-' . $i);
                    $cards[] = stripslashes($this->_request->getParam('card-'.$i));
                    if($type_data_ep == '0'){
                        $id_eps[] = "'".$model->cutId($this->_request->getParam('ep-' . $i))."'";
                    }
                }
            }
            if($type_data_ep == '0'){
                $url_eps = implode('|', $url_eps);
                $id_eps = implode(',',$id_eps);   
            }else{
                $url_eps = implode('#|#', $url_eps);
            }
            $des_eps = implode('#|#', $des_eps);
            $cards = implode('#|#', $cards);
            $url_mp3 = implode('#|#', $url_mp3);
           // Zend_Debug::dump($cards);die;
            $banner = $this->_request->getParam('banner');
            $check_banner = $this->_request->getParam('check-banner');
            if ($check_banner == 'on') {
                $banner = "ON|" . $this->_request->getParam('banner');
            }
            //Zend_Debug::dump($banner);die;
            $category = $this->_request->getParam('select-category');
            $name_type = $this->_request->getParam('select-type-' . $category);
            $name_type = implode('|', $name_type);
            $type = $category . "|" . $name_type;
            //$date=$this->_request->getParam('date');
            $title_vi = $this->_request->getParam('title_vi');
            $title_en = $this->_request->getParam('title_en');
            $year = $this->_request->getParam('year');
            $national = $this->_request->getParam('national');
            $description = $this->_request->getParam('description');
            $urlImage = $this->_request->getParam('urlSelectImg');
            $status = $this->_request->getParam('status');
            $source = $this->_request->getParam('source');
             $typeplay = $this->_request->getParam('typeplay');
             $keywords = $this->_request->getParam('keywords');
            $arrParam = array(
                'type' => $type,
                //'date'=>$date,
                'title_vi' => $title_vi,
                'title_en' => $title_en,
                'description' => $description,
                'year' => $year,
                'national' => $national,
                'number_ep' => $number_ep,
                'id_eps' => $id_eps,
                'url_eps' => $url_eps,
                'type_data_ep' => $type_data_ep,
                'des_eps' => $des_eps,
                'thumb' => $urlImage,
                'banner' => $banner,
                'post_by' => $this->_email,
                'source' => $source,
                'status' => $status,
                'typeplay' => $typeplay,
                'keywords' => $keywords,
                'card' => $cards,
                'mp3' => $url_mp3
            );
            $filter = new RemoveCircumflex();
            $title_vi_kd = $filter->filter($title_vi);
            $title_en_kd = $filter->filter($title_en);
            $des_eps_kd = $filter->filter($des_eps);
            $arrParam_kodau = array(
                'title_vi' => strtolower($title_vi_kd),
                'title_en' => strtolower($title_en_kd),
                'des_eps' => strtolower($des_eps_kd),
                );
            //Zend_Debug::dump($arrParam);die;
            $model = new FilmInfoModel();
            //$model->insert($arrParam);
            $id = $model->saveItem($arrParam, array("task" => "add"));
            $arrParam_kodau['id'] = $id;
            $model->saveAddItem($arrParam_kodau, array("task" => "add"));
            $this->_redirect('/admin/index/index');
        }
    }
    public function editAction()
    {
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            $filmModel = new FilmInfoModel();
            $films = $filmModel->getFilmById($id);
            if ($films[0]['post_by'] != $this->_email && $this->_role != 'Admin') {
                echo "<script type='text/javascript'>
   					alert( 'Bạn không có quyền truy cập trang này');
 			</script>";
                exit;
            }
            $this->view->film = $films;
        }
    }
    public function doneeditAction()
    {
        if ($this->_request->isPost()) {
            $model = new FilmInfoModel();
            $old_ep = $this->_request->getParam('old_ep');
            $add_ep = $this->_request->getParam('add_ep');
            $del_ep = $this->_request->getParam('del_ep');
            $type_data_ep = $this->_request->getParam('type_data_ep');
            $number_ep = $old_ep + $add_ep - $del_ep;
            
            for($i =1; $i <= $old_ep+$add_ep; $i++){
                if($this->_request->getParam('ep-' . $i)!= "" && $this->_request->getParam('ep-' . $i)!= null){
                    $url_eps[] = stripslashes($this->_request->getParam('ep-' . $i));
                                        $des_eps[] = $this->_request->getParam('des-' . $i);
                    $cards[] = stripslashes($this->_request->getParam('card-'.$i));
                    $url_mp3[] = stripslashes($this->_request->getParam('mp3-' . $i));

                    if($type_data_ep == '0'){
                        $id_eps[] = "'".$model->cutId($this->_request->getParam('ep-' . $i))."'";
                    }
                }
            }
            if($type_data_ep == '0'){
                $url_eps = implode('|', $url_eps); 
                $id_eps = implode(',',$id_eps);  
            }else{
                $url_eps = implode('#|#', $url_eps);
            }
            
            $des_eps = implode('#|#', $des_eps);
            $cards = implode('#|#', $cards);
            $url_mp3 = implode('#|#', $url_mp3);
        
            $banner = $this->_request->getParam('banner');
            $check_banner = $this->_request->getParam('check-banner');
            if ($check_banner == 'on') {
                $banner = "ON|" . $this->_request->getParam('banner');
            }
            $category = $this->_request->getParam('select-category');
            $name_type = $this->_request->getParam('select-type-' . $category);
            $name_type = implode('|', $name_type);
            $type = $category . "|" . $name_type;
            $id = $this->_request->getParam('id');
            //$date=$this->_request->getParam('date');
            $title_vi = $this->_request->getParam('title_vi');
            $title_en = $this->_request->getParam('title_en');
            $year = $this->_request->getParam('year');
            $national = $this->_request->getParam('national');
            $description = $this->_request->getParam('description');
            $urlImage = $this->_request->getParam('urlSelectImg');
            $status = $this->_request->getParam('status');
            $typeplay = $this->_request->getParam('typeplay');
            $source = $this->_request->getParam('source');
            $keywords = $this->_request->getParam('keywords');
            $arrParam = array(
                'id' => $id,
                'type' => $type,
                //'date'=>$date,
                'title_vi' => $title_vi,
                'title_en' => $title_en,
                'description' => $description,
                'year' => $year,
                'national' => $national,
                'number_ep' => $number_ep,
                'id_eps' => $id_eps,
                'url_eps' => $url_eps,
                'type_data_ep' => $type_data_ep,
                'thumb' => $urlImage,
                'banner' => $banner,
                'status' => $status,
                'typeplay' => $typeplay,
                'source' => $source,
                'keywords' => $keywords,
                'des_eps' => $des_eps,
                'card' => $cards,
                'mp3' => $url_mp3
            );
            
            //Zend_Debug::dump($arrParam);die;
            $filter = new RemoveCircumflex();
            $title_vi_kd = $filter->filter($title_vi);
            $title_en_kd = $filter->filter($title_en);
            $des_eps_kd = $filter->filter($des_eps);
            $arrParam_kodau = array(
                'id' => $id,
                'title_vi' => strtolower($title_vi_kd),
                'title_en' => strtolower($title_en_kd),
                'des_eps' => strtolower($des_eps_kd),
                );
            $model = new FilmInfoModel();
            //$model->update($arrParam,"id = ".$arrParam['id']);
            $id = $model->saveItem($arrParam, array("task" => "edit"));
            $model->saveAddItem($arrParam_kodau, array("task" => "edit"));
            $this->_redirect('/admin/index/index');
        }
    }
    public function deleteAction()
    {
        $rows = $this->_request->getParam('checks');
        $model = new FilmInfoModel();

        $model->multiDelete($rows);
        $this->_redirect('/admin/index/index');
        exit();
    }
    public function nominationAction()
    {
        $id = $_POST['data'];
        $nomination = $_POST['flag'];
        $model = new FilmInfoModel();
        $data = array('nomination' => $nomination);
        $where = array('id=?' => $id);
        $model->update($data,$where);
        $number = count($model->fetchAll("nomination = 1"));
        echo $number;
        die;
    }
    public function loginAction()
    {

        //set layout
        $layoutPath = TEMPLATE_PATH . "/admin";
        $option = array('layout' => 'login', 'layoutPath' => $layoutPath);
        Zend_Layout::startMvc($option);
        Zend_Loader::loadClass('LightOpenID');
        Zend_Loader::loadClass('AuthModel');
        $this->view->headTitle()->set('Ðăng nhập');
        $request = $this->getRequest();
        $view = $this->view;

        //set layout
        $layoutPath = TEMPLATE_PATH . "/admin";
        $option = array('layout' => 'login', 'layoutPath' => $layoutPath);
        Zend_Layout::startMvc($option);

        //check authenticated
        $auth = new AuthModel();
        if ($auth->hasIdentity())
            $this->_redirect('/admin/index');
        if ($request) {
            try {
                $openid = new LightOpenID;

                if (!$openid->mode) {
                    if ($request->getParam('login')) {
                        $openid->identity = 'https://www.google.com/accounts/o8/id';
                        $openid->required = array(
                            'namePerson/first',
                            'namePerson/last',
                            'contact/email');
                        header('Location: ' . $openid->authUrl());
                    }

                    //user cancel
                } elseif ($openid->mode == 'cancel') {
                    $warning_message = 'User has canceled authentication!';
                    $view->warning_messages = $warning_message;
                } else {
                    //login success
                    if ($openid->validate()) {
                        //gmail tra ve
                        $auth = new AuthModel();
                        $identity = $openid->identity;
                        $attributes = $openid->getAttributes();
                        $email = $attributes['contact/email'];
                        $first_name = $attributes['namePerson/first'];
                        $last_name = $attributes['namePerson/last'];
                        $adminModel = new AdminModel();
                        $check_email = $adminModel->get_email_admin($email);
                        //Zend_Debug::dump($check_email);die;
                        //xu ly
                        if ($auth->validateUser($email) && $check_email != null && $check_email != "") {
                            $auth->setIdentity($identity, $email, $first_name, $last_name);
                            $this->_redirect("/admin/index/index");
                        } else {
                            $message = "Can't login with $email";
                            $view->error_messages = $message;
                        }
                    } else {
                        $view->error_messages = $message;
                    }
                }
            }
            catch (ErrorException $e) {
                $message = $e->getMessage();
                $view->error_messages = $message;
            }
        }
    }
    public function logoutAction()
    {

        $aut = Zend_Auth::getInstance();
        $aut->clearIdentity();

        Zend_Loader::loadClass('AuthModel');
        $auth = new AuthModel();
        $auth->clearIdentity();
        $this->_redirect('/admin/index/login');
    }
    public function chuyenmucAction()
    {
        if (@$_REQUEST['ajax']) {
            $category = strip_tags($_REQUEST['category']);
            $model = new ParentTypeModel();
            $type = $model->getTypeByName($category);
            $type = $type[0]['name_types'];
            $type = explode('|', $type);
            $i = 0;
            while ($type[$i] != "" || $type[$i] != null) {
                $i++;
            }
            ;
            $this->view->type = $type;
            $this->view->num_type = $i;
        }
    }
}
